/**
 * @author David Milne
 */
var left = 0 ;
var right = 1 ;

function TraySlider() {
	logDebug("TraySlider created");
	
	var trayA ;
	var trayB ;
	var trayC ;
	
	var cmdSlideLeft ;
	var cmdSlideRight ;
	
	var isSliding ;
	var slidePosition ;	
	
	var currTrayWidth ;
}

TraySlider.prototype.init = traySlider_init ;
TraySlider.prototype.resize = traySlider_resize ;
TraySlider.prototype.slideLeft = traySlider_slideLeft ;
TraySlider.prototype.slideRight = traySlider_slideRight ;
TraySlider.prototype.animateSlide = traySlider_animateSlide ;



function traySlider_init(trayA, trayB, trayC) {
	try {
		this.id = "traySlider" ;
		
		this.trayA = trayA ;
		this.trayB = trayB ;
		this.trayC = trayC ;
		
		this.appendChild(trayA) ;
		this.appendChild(trayB) ;
		this.appendChild(trayC) ;	
		
		this.cmdSlideLeft = document.createElement("div") ;
		this.cmdSlideLeft.className = "sliderButton" ;
		this.cmdSlideLeft.style.left = "0px" ;
		this.cmdSlideLeft.onclick = new Function("koru.traySlider.slideLeft();") ;
		this.appendChild(this.cmdSlideLeft) ;
		
		var border_t = document.createElement("div") ;
		border_t.className = "border_t"
		this.cmdSlideLeft.appendChild(border_t) ;
		
		var border_b = document.createElement("div") ;
		border_b.className = "border_b"
		this.cmdSlideLeft.appendChild(border_b) ;
		
		var arrow = document.createElement("div") ;
		arrow.className = "arrow_l"
		this.cmdSlideLeft.appendChild(arrow) ;
		
		this.cmdSlideRight = document.createElement("div") ;
		this.cmdSlideRight.className = "sliderButton" ;
		this.cmdSlideRight.style.right = "0px" ;
		this.cmdSlideRight.onclick = new Function("koru.traySlider.slideRight();") ;
		this.appendChild(this.cmdSlideRight) ;
		
		border_t = document.createElement("div") ;
		border_t.className = "border_t"
		this.cmdSlideRight.appendChild(border_t) ;
		
		border_b = document.createElement("div") ;
		border_b.className = "border_b"
		this.cmdSlideRight.appendChild(border_b) ;
		
		arrow = document.createElement("div") ;
		arrow.className = "arrow_r"
		this.cmdSlideRight.appendChild(arrow) ;
		
		
		
		this.isSliding = false ;
		this.slidePosition = left ;
		trayC.style.display = "none" ;
		this.cmdSlideLeft.style.display = "none" ;
				
	} catch ( e ) { 
		logError("slidingPane.init.error: " + e ); 
		throw (e) ;
	}
} 
  
function traySlider_resize(width, height) {
	try {
		this.style.width = width + "px" ;
		this.style.height = height + "px" ;
		
		this.currTrayWidth = (width-50)/2 ;
		
		this.trayA.resize(this.currTrayWidth, height) ;
		this.trayB.resize(this.currTrayWidth, height) ;
		this.trayC.resize(this.currTrayWidth, height) ;
		
		 
		this.cmdSlideLeft.style.height = height - 4 + "px";
		this.cmdSlideRight.style.height = height - 4 + "px" ;
		
		if (this.slidePosition == left) {
			this.trayA.style.left = "0px" ;
			this.trayB.style.left = (this.currTrayWidth + 10) + "px" ;
			this.trayC.style.left = (this.currTrayWidth * 2 + 20) + "px" ;			
		}
		
	} catch (e) {
		logError("slidingPane.resize.error: " + e) ;
		throw (e) ;
	}
}

function traySlider_slideLeft() {
	try {
		this.sliding = true ;
		this.slidePosition = left ;
		
		this.cmdSlideLeft.style.display = "none" ;
		this.trayA.style.display = "" ;
		
		this.animateSlide(0, 30-this.currTrayWidth, 0) ;
	
	} catch (e) {
		logError("slidingPane.slideRight.error: " + e) ;
		throw (e) ;
	}
}

function traySlider_slideRight() {
	try {
		this.sliding = true ;
		this.slidePosition = right ;
		
		
		this.cmdSlideRight.style.display = "none" ;
		this.trayC.style.display = "" ;
		
		
		this.animateSlide(0, 0, 30-this.currTrayWidth) ;
	
	} catch (e) {
		logError("slidingPane.slideRight.error: " + e) ;
		throw (e) ;
	}
}

function traySlider_animateSlide(x, startPos, endPos) {
	try {
		
		var posDiff = endPos - startPos ;
		if (Math.PI - x > 0.25) {
			
  			x = x + 0.25 ;
   			var y = (0.5 * (Math.sin(x-(Math.PI/2)))) + 0.5 ;
  
 	  		pos = Math.round(startPos + (y*posDiff)) ;
			logDebug("sliding " + pos) ;
			
			this.trayA.style.left = pos + "px" ;
			this.trayB.style.left = pos + this.currTrayWidth + 10 +  "px" ;
			this.trayC.style.left = pos + (2*this.currTrayWidth) + 20 + "px" ;
			
			//this.trayC.style.left = pos + "px" ;
			
			setTimeout("koru.traySlider.animateSlide(" + x + "," + startPos + "," + endPos + ")", 50) ;
		} else {
			if (this.slidePosition == left) {
				this.trayC.style.display = "none" ;
				this.cmdSlideRight.style.display = "" ;
			} else {
				this.trayA.style.display = "none" ;
				this.cmdSlideLeft.style.display = "" ;
			}
		}
			
	} catch (e) {
		logError("traySlider.animateSlide.error: " + e) ;
		throw (e) ;
	}
}
